const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

//获取应用实例
const app = getApp()
Page({
  data: {
    newGoods: [],
    hotGoods: [],
    topics: [],
    brands: [],
    floorGoods: [],
    banner: [],
    channel: []
  },
  onShareAppMessage: function () {
    return {
      title: 'NideShop',
      desc: '仿网易严选微信小程序商城',
      path: '/pages/index/index'
    }
  },

  getIndexData: function () {
    let that = this;
    util.request(api.IndexUrl).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          newGoods: res.data.newGoodsList,
          hotGoods: res.data.hotGoodsList,
          topics: res.data.topicList,
          brand: res.data.brandList,
          floorGoods: res.data.categoryList,
          banner: res.data.banner,
          channel: res.data.channel
        });
      }
    });
  },
  userLogin: function(){
      wx.login({
          success: function (res) {
              console.log("code**************************")
              console.log(res);
              if (res.code) {
                  //就是在这里发起网络请求，使用wx.request()，将登陆态发送给自家的服务器上
                    wx.request({
                        url: 'https://api.weixin.qq.com/sns/jscode2session',
                        data: {
                            appid: 'wxbbadfefa3b10b77c',
                            secret: '85697a5a8a11da3429812c4a0b69958c',
                            js_code:res.code,
                            grant_type:'authorization_code',
                        },
                        header: {
                            'content-type': 'application/json' // 默认值
                        },
                        success: function (res) {
                            console.log(res.data)
                            try {
                                wx.setStorageSync('openid', res.data.openid)
                                console.log('缓存成功**************************');
                            } catch (e) {
                            }
                        }
                    })
              } else {
                  console.log('获取用户登录态失败！' + res.errMsg)
              }
          },
          fail: function () {
              console.log("启用wx.login函数，失败！");
          },
          complete: function () {
              console.log("已启用wx.login函数");
          }
      });

  },
  onLoad: function (options) {
    this.getIndexData();
    this.userLogin();

  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this.getIp();
      try {
          var value = wx.getStorageSync('openid')
          if (value) {
              // Do something with return value
              console.log(value);
          }
      } catch (e) {
          // Do something when catch error
      }
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  getIp:function(){
      function GetLocalIPAddress() {
          var obj = null;
          var rslt = "";
          try {
              obj = new ActiveXObject("rcbdyctl.Setting");
              rslt = obj.GetIPAddress;
              console.log(rslt);
              obj = null;
          }
          catch (e) {
              //异常发生
              console.log('异常**********************')
          }
          return rslt;
      }
    //   document.write("你的IP是：" + GetLocalIPAddress());
      console.log('ip:' + GetLocalIPAddress());
  },
})
